/*=============================================================================

MOTION INTERFACE MODULE

FILE: HMot.h

DESCRIPTION

Motion interface base module definition

EXTERNALIZED FUNCTIONS


Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

EDIT HISTORY

---------  -------  -------------------------------------------------------
when       who     what, where, why
---------  -------  -------------------------------------------------------
07/17/04   nso4th   Frame Grabber Interface

=============================================================================*/

#ifndef _HMOT_H_
#define _HMOT_H_

/*=============================================================================
HEADER INCLUSION
=============================================================================*/
#include <mxBase.h>
#include <MOTFTbl.h>

/*=============================================================================
ENUMERATORS
=============================================================================*/

typedef enum
{
	HMOT_NONE_S = 0x00,
	HMOT_INIT_S = 0x01, // Initialized but homming was not done
	HMOT_HOME_S = 0x02,  // Detecting the origin
	HMOT_NORMAL_S = 0x03, // Normal state
	HMOT_PATH_S = 0x04,  // Detecting the path

	HMOT_DUMMY_S = 0xFF
} HMotState_e;

typedef enum
{
	HMOT_HOME_NONE_S = 0x00,
	HMOT_HOME_TRANSIT_S = 0x01,	
	HMOT_HOME_DONE_S = 0x02
} HMotHomeState_e;

/*=============================================================================
CALLBACK DEFINITION
=============================================================================*/

/*=============================================================================
STRUCTURE DEFINITION
=============================================================================*/

/*=============================================================================
HFgDevBase CLASS DEFINITION
=============================================================================*/
class HMot : public HDev
{
public:
	HMot();
	~HMot();

	/*-----------------------------------------------------------------------------
	PUBLIC FUNCTIONS
	-----------------------------------------------------------------------------*/
	/*---------------------------------------------------------
	BASIC OPERATION
	---------------------------------------------------------*/
public:
	boolean Init(char *pszcfg);
	boolean Close(void);

	// Error Access
	dword Get_LastError(void);
	char *Get_ErrorString(dword err_code);

	dword GetState(void);

	/*---------------------------------------------------------
	MOTION CONTROL
	---------------------------------------------------------*/
public:
	// Signal
	boolean SigGet(int32 axID, dword sig, boolean *on);

	boolean ServoOn(int32 axID, boolean bOn);
	boolean IsServoOn(int32 axID);

	boolean SetOrigin(int32 axID);

	boolean MoveContinue(int32 axID, byte byDirection);
	boolean Move(int32 axID, double64 dPos);
	boolean IsMoveDone(int32 axID);
	boolean MoveOverride(int32 axID, double64 dPos);

	boolean EStop(boolean on);
	boolean Stop(int32 axID);

	boolean Home(void);
	boolean Home(int32 axID);
	boolean HomeStop(void);
	boolean HomeStop(int32 axID);
	boolean IsHomeDone(void);
	boolean IsHomeDone(int32 axID);
	boolean HomeClear(void);
	boolean HomeClear(int32 axID);

	boolean IsHomming(void);
	boolean IsHomming(int32 axID);

	boolean Path(mcxt mPath);
	boolean PathStop(void);
	boolean PathLock(void);
	boolean PathUnlock(void);
	boolean IsPathMoving(void);

	// User IO
	boolean UserOutput(int32 axID, int32 iChan, boolean bOn);
	boolean UserOutputGet(int32 axID, int32 iChan);

	boolean UserInput(int32 axID, int32 iChan);

	// In simulatin mode
	boolean UserInput_On(boolean on);
	/* Input */
	boolean IsInput(dword dwChan);
	boolean Input32(dword *pIn);
	boolean Input32Ex(dword modID, dword *pIn);

	/* Output */
	boolean IsOutput(dword dwChan);
	boolean IsOutput32(dword *pOut);
	boolean IsOutput32Ex(dword modID, dword *pOut);
	boolean Output(dword dwChan, boolean bOn);
	boolean Output32(dword dwOut);
	boolean Output32Ex(dword modID, dword dwOut);

	// Trigger
	boolean TrigEnable(int32 axID, boolean bEnable);
	boolean TrigSetTable(int32 axID, double64 dStart, double64 dEnd, double64 dInterval);
	boolean TrigStart(int32 axID);
	boolean TrigStop(int32 axID);
	boolean TrigInvert(int32 axID, boolean bEnable);

	// Encoder direction
	boolean EncInvert(int32 axID, boolean bEnable);

	// Alarm
	boolean IsAlarm(int32 axID);
	boolean ClearAlarm(int32 axID);

	boolean IsEMO(dword emoPos);
	boolean LoadRateGet(int32 axID, int32 *iRate);

	// E-GEAR
	boolean EgearSet(int32 axID, int32 axMaster, double64 ratio);
	boolean EgearIsSet(int32 axID);

	// LIMIT
	boolean SWenable(int32 axID, boolean enable);
	boolean IsSWenable(int32 axID);
	boolean SWset(int32 axID, double64 negPos, double64 posPos);
	boolean SWget(int32 axID, double64 *negPos, double64 *posPos);

	// Home Offset
	boolean HOMEset(int32 axID, double64 Hoffset);
	boolean HOMEget(int32 axID, double64 *Hoffset);

	/*---------------------------------------------------------
	PARAMETER & STATUS INTERFACE
	---------------------------------------------------------*/
public:
	int32 GetAxisNum(void);

	boolean SetVelocity(int32 axID, double64 dVel);
	double64 GetVelocity(int32 axID);

	boolean SetAccel(int32 axID, double64 dAccel);
	double64 GetAccel(int32 axID);

	boolean SetHomeSpeed(int32 axID, double64 dSpeed);

public:
	boolean GetPosition(int32 axID, double64 *pPos);	
	boolean SetPosition(int32 axID, double64 dPos);
	boolean GetPositionCommanded(int32 axID, double64 *pPos);
	boolean GetVelocityActual(int32 axID, double64 *dVel);

public:
	mcxt Get_MotCxt(void);

	/*---------------------------------------------------------
	PROTECTED MEMBERS
	---------------------------------------------------------*/
protected:
	int32		m_axNum;
	dword		m_motState, m_homeState;
	word		m_wHomeReqMask, m_wHomeMask, m_wHomeDoneMask;
	double64		*m_tblVel, *m_tblAccel, *m_tblPos;
	double64		*m_tblSWn, *m_tblSWp; // SW-Limit
	boolean		*m_tblSW;
	MOTFTbl		*m_motFTbl;

protected:
	dword SetState(dword newState);
	void SetHomeState(dword homeState);
	void SetAxisNum(int32 axNum);

	double64 VEL_CHECK(int32 axID);
	double64 ACCEL_CHECK(int32 axID);

	//-------------------------------------
	// LIB MODE
	//-------------------------------------
protected:
	boolean INIT_LIB(char *pszname);
	boolean CLOSE_LIB(void);
	//-------------------------------------
	// ERROR HANDLER
	//-------------------------------------
protected:
	char					m_pszErr[128];
	dword				m_errCode;

	void ERR_RESET(void);
	void ERR_SET(dword err_code);

	/*-------------------------------------------------------------------
	EVENT HANDLER
	-------------------------------------------------------------------*/
public:
	virtual dword Event_Handler(dword dwEvent, dword wParam, dword lParam);
protected:
	virtual dword Event_PreProc(dword dwEvent, dword wParam, dword lParam);

};
#endif /* _HMOTBASE_H_ */

/*--- END OF HMot.h ---*/
